package com.huonan.test.test2;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Main {
    public static void main(String[] args) {
        List<User> list = getList(); // 假设已有一个User对象的List集合

        Set<String> nameSet = new HashSet<>();
        List<User> distinctList = new ArrayList<>();

        for (User user : list) {
            if (nameSet.add(user.getName())) {
                distinctList.add(user);
            }
        }

        distinctList.forEach(System.out::println);
    }

    private static List<User> getList() {
        // 模拟获取User对象的List集合
        List<User> list = new ArrayList<>();
        list.add(new User("张三", "1"));
        list.add(new User("李四", "2"));
        list.add(new User("张三", "3")); // 重复的name
        list.add(new User("王五", "4"));
        list.add(new User("李四", "5")); // 重复的name
        return list;
    }

    private static class User {
        private String name;
        private String id;

        public User(String name, String id) {
            this.name = name;
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
        
        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", id='" + id + '\'' +
                    '}';
        }
    }
}